package com.suozhang.framework.component.websocket;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.suozhang.framework.component.websocket.listener.WsStatusListener;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Response;
import okio.ByteString;

/**
 * @ProjectName: ZhiYuanCar
 * @Description: java类作用描述
 * @Author: lijuwen
 * @CreateDate: 2019/4/24 11:11
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/4/24 11:11
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class WsService extends Service {

    private static final String TAG = WsService.class.getSimpleName();

    public static final String WEBSOCKET_ACTION = "WEBSOCKET_ACTION";

    private BroadcastReceiver connectionReceiver;

    private WsManager wsManager;


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (connectionReceiver == null) {
            connectionReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    conntedSocket();
                    IntentFilter intentFilter = new IntentFilter();
                    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
                    registerReceiver(connectionReceiver, intentFilter);
                }
            };
        }

        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void conntedSocket() {
        if (wsManager != null) {
            wsManager.stopConnect();
            wsManager = null;
        }
        wsManager = new WsManager.Builder(getBaseContext()).client(
                new OkHttpClient().newBuilder()
                        .pingInterval(15, TimeUnit.SECONDS)
                        .retryOnConnectionFailure(true)
                        .build())
                .needReconnect(true)
                .wsUrl(WSHost.WSURL)
                .build();
        wsManager.setWsStatusListener(wsStatusListener);
        wsManager.startConnect();
    }

    private WsStatusListener wsStatusListener=new WsStatusListener() {
        @Override
        public void onOpen(Response response) {
            super.onOpen(response);
        }

        @Override
        public void onMessage(String text) {
            super.onMessage(text);
        }

        @Override
        public void onMessage(ByteString bytes) {
            super.onMessage(bytes);
        }

        @Override
        public void onReconnect() {
            super.onReconnect();
        }

        @Override
        public void onClosing(int code, String reason) {
            super.onClosing(code, reason);
        }

        @Override
        public void onClosed(int code, String reason) {
            super.onClosed(code, reason);
        }

        @Override
        public void onFailure(Throwable t, Response response) {
            super.onFailure(t, response);
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (wsManager != null) {
            wsManager.stopConnect();
            wsManager = null;
        }
    }


}

